package com.wang.vedio.dialog;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;

import com.wang.vedio.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by JY on 2018/4/11.
 */

public class DialogUtil {


    private Dialog dialog;
    private RecyclerView rec;
    private DialogAdapter mAdapter;
    private int pos;
    private OnItemClickListener mOnItemClickListener;
    private OnPayClickListener mOnPayClickListener;//支付
    private OnShareClickListener mOnShareClickListener;//分享

    //获取单例
    public static DialogUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }


    public void setOnShareClickListener(OnShareClickListener onShareClickListener) {
        this.mOnShareClickListener = onShareClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setBottomDialog(Activity activity, Dialog fullDialog, View view) {

        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
//        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(view);
    }


    public void setWindow1(Dialog dialog, View view, Activity activity) {
        Window window = dialog.getWindow();
        window.setDimAmount(0f);
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setContentView(view);
    }

    /**
     * @param path 网络图片地址
     */
    public Bitmap getBitmap(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }

    public View showFullDialog(Activity activity, Dialog fullDialog, int layout) {

        View dialogView = LayoutInflater.from(activity).inflate(layout, null);
        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        //window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        //window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(dialogView);

        return dialogView;
    }

    public View ChoiceImgDialog(Activity activity, Dialog dialog, int layout) {

        View dialogView = LayoutInflater.from(activity).inflate(layout, null);
        //获得dialog的window窗口
        Window window = dialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(10, 0, 10, 10);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setContentView(dialogView);
        return dialogView;
    }

    public void setDialog(Activity activity, AlertDialog fullDialog, View view, int gravity, int padding) {

        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
//        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(gravity);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        //window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(padding, 0, padding, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(view);
    }

    public void setDialog1(Activity activity, AlertDialog fullDialog, View view, int gravity, int padding) {

        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(gravity);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        //window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(padding, 0, padding, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(view);
    }

    public void setDialog2(Activity activity, Dialog fullDialog, View view, int gravity, int padding) {

        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(gravity);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        //window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(padding, 0, padding, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(view);
    }

    public void setDialog3(Activity activity, Dialog fullDialog, View view, int gravity, int padding) {

        //获得dialog的window窗口
        Window window = fullDialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(gravity);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        //window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(padding, 0, padding, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        fullDialog.setContentView(view);
    }

    public void showDatePicker(Activity activity, final TextView tv) {
        Calendar cale1 = Calendar.getInstance();
        new DatePickerDialog(activity, DatePickerDialog.THEME_DEVICE_DEFAULT_DARK, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                //这里获取到的月份需要加上1哦~
                tv.setText(+year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        }
                , cale1.get(Calendar.YEAR)
                , cale1.get(Calendar.MONTH)
                , cale1.get(Calendar.DAY_OF_MONTH)).show();
    }


    public void setWindow(Dialog dialog, View view, Activity activity) {
        Window window = dialog.getWindow();
        WindowManager wm = activity.getWindowManager();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialogStyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setContentView(view);
    }

    public void setOnPayClickListener(OnPayClickListener onPayClickListener) {
        this.mOnPayClickListener = onPayClickListener;
    }

    public interface OnShareClickListener {
        void qqOnClick(View v);

        void circleOnClick(View v);

        void qZoneOnClick(View v);

        void weixinOnClick(View v);

        void jubaoOnClick(View v);

    }

    public interface OnPayClickListener {
        void onYuE(View v);//支付宝支付

        void onAliPay(View v);//支付宝支付

        void onWeiXinPay(View v);//微信支付
    }

    public interface OnItemClickListener {
        void onClick(View v, int position);


    }

    private static class SingletonHolder {
        private static final DialogUtil INSTANCE = new DialogUtil();
    }
}
